package cn.tj212.xiaoyin.common.web;


import cn.tj212.xiaoyin.common.web.auth.SessionUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;


import javax.servlet.http.HttpServletRequest;

public class SessionContext {

    public static final String IDENTIFY_CODE_KEY = "_consts_identify_code_key_";//其他人不得占用
    public static final String AUTH_USER_KEY = "_consts_auth_user_key_";//其他人不得占用

    public static Long getUserId() {
        if (null != getAuthUser()) {
            return getAuthUser().getUserId();
        }
        return null;
    }

    public static String getUsername() {
        if (null != getAuthUser()) {
            return getAuthUser().getUsername();
        }
        return null;
    }

    public static Long getWxUserId(HttpServletRequest request) {
        if (null != getWxAuthUser(request)) {
            return getWxAuthUser(request).getUserId();
        }
        return null;
    }

    public static String getWxUsername(HttpServletRequest request) {
        if (null != getWxAuthUser(request)) {
            return getWxAuthUser(request).getUsername();
        }
        return null;
    }

    public static boolean isWxLogin(HttpServletRequest request) {
        return null != getWxAuthUser(request);
    }

    /**
     * 微信登录获取当前用户
     *
     * @param request
     * @return
     */
    public static SessionUser getWxAuthUser(HttpServletRequest request) {
        String sessionId = request.getSession().getId();
        Object obj = SessionContext.getAttribute(request, sessionId);
        if (null == obj) {
            return (SessionUser) obj;
        }
        return null;
    }

    public static SessionUser getAuthUser() {
        if (null != SecurityUtils.getSubject().getPrincipal()) {
            return (SessionUser) SecurityUtils.getSubject().getPrincipal();
        }
        return null;
    }

    /**
     * 获取验证码
     *
     * @param request
     * @return
     */
    public static String getIdentifyCode(HttpServletRequest request) {
        if (request.getSession().getAttribute(IDENTIFY_CODE_KEY) != null) {
            return getAttribute(request, IDENTIFY_CODE_KEY).toString();
        } else {
            return null;
        }
    }

    public static Object getAttribute(HttpServletRequest request, String key) {
        return request.getSession().getAttribute(key);
    }

    public static void setAttribute(HttpServletRequest request, String key, Object value) {
        request.getSession().setAttribute(key, value);
    }

    public static void removeAttribute(HttpServletRequest request, String key) {
        request.getSession().removeAttribute(key);
    }

    public static boolean isLogin() {
        Subject currentUser = SecurityUtils.getSubject();
        if (null != currentUser && null != currentUser.getPrincipal()) {
            return true;
        }
        return false;
    }

    public static void shiroLogout() {
        Subject currentUser = SecurityUtils.getSubject();
        try {
            currentUser.logout();
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }
    }
}
